const app = getApp();
var {Api} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flage:false,//是否公开
    textarea:'',//笔记内容
    id:'',//题目id
    images:[],//上传的图片
    title:'',//题目title
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      title:app.title
    })
    console.log(options)
  },
  chooseimg(){//上传图片
    var that = this
    wx.chooseImage({
      count: 10,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(res)
        wx.showLoading({
          title: '加载中',
        })
       for(let i = 0;i<tempFilePaths.length;i++){
        wx.uploadFile({
          url: wx.$get.upload_img,
          filePath: tempFilePaths[i],
          name: "file",
          formData: {
            key: wx.$cache.get('key')
          },
          success: (result) => {
            wx.hideLoading()
            console.log(result)
            const item = JSON.parse(result.data)
            console.log(item)
            that.setData({
              images:that.data.images.concat(item.datas.url)
            })
          },
        });
       }
      }
    })
  },
  switch(){
    this.setData({
     flage:!this.data.flage 
    })
  },
  bindinput(e){//笔记内容
    this.setData({
      textarea:e.detail.value
    })
  },
  addNote(){//提交报错
    var that = this
    let imgs = this.data.images.toString()
    console.log(imgs)
    var params = {
      subject_id:this.data.id,
      content:this.data.textarea,
      imgs,
    }
    wx.$http.get(wx.$get.report_errors,params).then(res=>{
      console.log(res,'报错')
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 1500
      })
      setTimeout(()=>{
        wx.navigateBack({
          delta: 1,
        })
      },1500)
    })
  },
  closefu(e){//删除照片
    const {index} = e.currentTarget.dataset
    let {images} = this.data
    images.splice(index,1);
    this.setData({
      images
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})